package com.rainng.coursesystem.repository;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class CommentRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 查询特定帖子下的所有评论
    public List<Map<String, Object>> findByPostId(int postId) {
        String sql = "SELECT comment_id, post_id, student_id, content, created_at " +
                "FROM rc_comment " +
                "WHERE post_id = ? AND deleted_at IS NULL";
        return jdbcTemplate.queryForList(sql, postId);
    }

    // 添加评论
    public int addComment(int postId, int studentId, String content) {
        String sql = "INSERT INTO rc_comment (post_id, student_id, content) VALUES (?, ?, ?)";
        return jdbcTemplate.update(sql, postId, studentId, content);
    }

    public int deleteComment(int commentId) {
        String sql = "DELETE FROM rc_comment WHERE comment_id = ?";
        return jdbcTemplate.update(sql, commentId);
    }

}